<template>
    <div style="display: flex; height: 60px; line-height: 60px; border-bottom: 1px solid #eee">
      <div style="width: 200px;display: flex;padding-left: 20px" @click="$router.push('/front')">
        <div style="width: 60px">
          <img  src="../../assets/logo.png" alt="" style="width: 30px; position: relative; top: 10px;left: 20px"/>
        </div>
        <div style="flex: 1">
          考研交流系统
        </div>
      </div>
      <div style="flex: 1">

<!--        <ul style="list-style: none;">-->
<!--          <li class="item"><a href="/login">菜单1</a></li>-->
<!--          <li class="item">-->
<!--            <el-dropdown>-->
<!--      <span class="el-dropdown-link" size="medium">-->
<!--        下拉菜单<i class="el-icon-arrow-down el-icon&#45;&#45;right"></i>-->
<!--      </span>-->
<!--              <el-dropdown-menu slot="dropdown">-->
<!--                <el-dropdown-item>黄金糕</el-dropdown-item>-->
<!--                <el-dropdown-item>狮子头</el-dropdown-item>-->
<!--                <el-dropdown-item>螺蛳粉</el-dropdown-item>-->
<!--                <el-dropdown-item>双皮奶</el-dropdown-item>-->
<!--                <el-dropdown-item>蚵仔煎</el-dropdown-item>-->
<!--              </el-dropdown-menu>-->
<!--            </el-dropdown>-->
<!--          </li>-->
<!--          <li class="item">菜单1</li>-->
<!--          <li class="item">菜单1</li>-->
<!--        </ul>-->

        <el-menu :default-active="'/home'" class="el-menu-demo" mode="horizontal" router>
          <el-menu-item index="/front/home">首页</el-menu-item>
          <el-menu-item index="/front/article" >文章查询</el-menu-item>
          <el-menu-item index="/front/message" >消息中心 <i style="display: block;background-color: #F56C6C; color: white; width: 12px;height: 12px; line-height: 12px;border-radius: 50%;font-size: 8px ;position: relative;bottom: 44px; left: 56px" v-if="message != 0"/></el-menu-item>
          <el-menu-item index="/front/collect">收藏管理</el-menu-item>
          <el-menu-item index="/front/articleEdit">编写文章</el-menu-item>
          <el-submenu index="2">
            <template slot="title">我的工作台</template>

            <div v-for="item in menus" :key="item.id">
              <div v-if="item.path">
                <el-menu-item :index="item.path">
                  <i :class="item.icon"></i>
                  <span slot="title"> {{item.name}} </span>
                </el-menu-item>
              </div>
            </div>
          </el-submenu>
        </el-menu>
      </div>
      <div  style="width: 160px">
        <div v-if="!user.username" style="text-align: right;padding-right: 5px">
          <el-button @click="$router.push('/login')">登录</el-button>
          <el-button @click="$router.push('/register')">注册</el-button>
        </div>
        <div v-else>
         <el-dropdown style="cursor: pointer">
           <div style="display: inline-block">
             <el-avatar @error="errorHandler"
                        style="width: 35px; border-radius: 50%; position: relative; top:10px; right: 5px; height: 35px">
               <img :src="user.avatarUrl" alt="" style="width: 35px; height: 35px">
             </el-avatar>
             <span>{{ user.nickname }}</span>
             <i class="el-icon-arrow-down ml-5" ></i>
           </div>
           <el-dropdown-menu slot="dropdown" style="width: 100px;text-align: center">
             <el-dropdown-item style="font-size: 14px; padding: 5px 0">
               <p @click="toPerson">个人信息</p>
             </el-dropdown-item>
             <el-dropdown-item style="font-size: 14px; padding: 5px 0">
               <p @click="toMy">我的</p>
             </el-dropdown-item>
<!--             <el-dropdown-item style="font-size: 14px; padding: 5px 0" v-if="this.user.role != 'ROLE_USER'">-->
<!--               <p @click="toHome">后端</p>-->
<!--             </el-dropdown-item>-->
             <el-dropdown-item>
               <span style="text-decoration: none" @click="logout">退出</span>
             </el-dropdown-item>
           </el-dropdown-menu>
         </el-dropdown>
       </div>
      </div>
    </div>

</template>

<style>
.item {
  display: inline-block;
  width: 100px;
}
.item a {
  color: #333333;
  text-decoration: none;
  background-color: transparent;
}
</style>

<script>

import {resetRouter} from "@/router";
import user from "@/views/User.vue";

export default {
  name: "FrontHeader",
  data() {
    return {
      message: 0,
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      menus: localStorage.getItem("menus") ?  JSON.parse(localStorage.getItem("menus")) : {}
    }
  },
  created() {
    this.loadMessage()
  },
  methods: {
    loadMessage() {
      if(user.id) {
        this.request.get("/message/myNewMessage").then(res => {
          if (res.code === '200') {
            this.message = res.data
          }
        })
      }
    },
    logout() {
      localStorage.removeItem("user")
      localStorage.removeItem("menus")
      // 重置路由
      resetRouter()
      this.$router.push("/login")
      this.$message.success("退出成功")
    },
    errorHandler() {
      return true
    },
    toPerson() {
      this.$router.push("/front/person")
    },
    toHome() {
      this.$router.push("/")
    },
    toMy() {
      this.$router.push('/front/user?id=' + this.user.id)
    }
  }
}
</script>